//
//  RCView.m
//  OCUIBootcamp
//
//  Created by 周晓旺 on 2023/11/29.
//

#import "RCView.h"
#import <UIKit/UIKit.h>
@implementation RCView

//MARK: - 处理超出父视图的子View 的点击事件
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    NSString *touchPoint = NSStringFromCGPoint(point);
    NSString *frame = NSStringFromCGRect(self.frame);
    NSString *bounds = NSStringFromCGRect(self.bounds);
    NSString *extraFram = NSStringFromCGRect(self.extraFrames);
    NSLog(@"----RCView的 pointInside方法----");
    NSLog(@"point = %@ ---- \n bounds = %@ \n frame = %@ ---- \n extraFrame = %@ ----", touchPoint, bounds, frame, extraFram);
    NSLog(@"point是否在bounds里 = %d",CGRectContainsPoint(self.bounds, point) || CGRectContainsPoint(self.extraFrames, point));
    NSLog(@"point是否在frame里 = %d",CGRectContainsPoint(self.frame, point) || CGRectContainsPoint(self.extraFrames, point));
    NSLog(@"----RCView的 pointInside方法----end");
    //bounds是相对于自身来说的，frame是相对于父视图来说的(并不是相对于window来说的)
    //pointInside对于当前视图本身 是相对于bounds来说的，看点击的point是否在自己的bounds里，
    //若使用self.frame 会导致范围错误，可能返回不在自己范围内，这样设置的话 [super hitTest:point withEvent:event]也会返回nil
    return  CGRectContainsPoint(self.bounds, point) || CGRectContainsPoint(self.extraFrames, point);
//    return  CGRectContainsPoint(self.frame, point) || CGRectContainsPoint(self.extraFrame, point);
}
//MARK: - 处理扩大范围的btn
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    //坐标相对于子视图来说，
    NSLog(@"----RCView的 hitTest方法----");
    NSString *touchPoint = NSStringFromCGPoint(point);
    NSString *expandedFrame = NSStringFromCGRect(self.expandedFrame);
    NSLog(@"point = %@ \n expandedFrame = %@",touchPoint, expandedFrame);
    NSLog(@"hitpoint 是否在expandedFrame里 = %d",CGRectContainsPoint(self.expandedFrame, point));
    NSLog(@"----RCView的 hitTest方法---- end");
    
    if (CGRectContainsPoint(self.expandedFrame, point)) {
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:[UIButton class]] && subView.tag == 100) {
                NSLog(@"子btn的frame = %@",NSStringFromCGRect(subView.frame));
                NSLog(@"子btn的bounds = %@",NSStringFromCGRect(subView.bounds));
                    return subView;
            }
        }
    }
    UIView *testView = [super hitTest:point withEvent:event];
    return [super hitTest:point withEvent:event];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
